package com.radar.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.radar.common.DateConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.*;

import java.io.File;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;

@Slf4j
public abstract class BaskTask implements TaskInterface {

    protected String rootPath = "";

    protected String processPath = "";

    @Value("${radar.ok-path:''}")
    protected String okPath = "";

    @Value("${radar.log-path:''}")
    protected String logPath = "";

    protected boolean isOpen = true;

    protected int handleHour = -1;

    protected int cleanTime = -1;

    public boolean filterDir() {
        if (!this.isOpen) {
            return false;
        }
        if (this.rootPath == null || this.rootPath.equals("")) {
            return false;
        }
        return FileUtil.isDirectory(this.rootPath);
    }

    public void cleanJob() {
        if (cleanTime == -1) {
            return;
        }
        cleanTimeOut(this.processPath, (long) (cleanTime + 1) * 1000 * 60 * 60 * 24);
    }

    public void cleanOKJob(String OKPath) {
        if (cleanTime == -1) {
            return;
        }
        cleanTimeOut(OKPath, (long) (cleanTime + 2) * 1000 * 60 * 60 * 24);
    }

    /**
     * 清理目录中过期的文件
     *
     * @param dirPath 文件路径
     * @param ms      时间 /毫秒
     */
    public void cleanTimeOut(String dirPath, long ms) {
        // 该日期之前的文件
        Date pointDate = new Date(System.currentTimeMillis() - ms);
        // 文件过滤条件
        IOFileFilter timeFileFilter = FileFilterUtils.ageFileFilter(pointDate, true);
        IOFileFilter fileFiles = new AndFileFilter(FileFileFilter.FILE, timeFileFilter);
        File directory = new File(dirPath);
        Iterator<File> itFile = FileUtils.iterateFiles(directory, fileFiles, TrueFileFilter.INSTANCE);
        // 删除符合条件的文件
        while (itFile.hasNext()) {
            itFile.next().delete();
        }
    }

    public void log(String content, String relativePath) {
        String logFile = this.logPath + File.separator + relativePath + File.separator + DateUtil.format(new Date(), DateConstants.DATE_FORMAT_YYYYMMDD) + ".log";
        FileUtil.touch(logFile);
        FileUtil.appendUtf8Lines(Collections.singletonList(DateUtil.format(new Date(), DateConstants.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS) + "  info: " + content), logFile);
        log.info(content);
    }

    public void error(String content, String relativePath) {
        String logFile = this.logPath + File.separator + relativePath + File.separator + DateUtil.format(new Date(), DateConstants.DATE_FORMAT_YYYYMMDD) + ".log";
        FileUtil.touch(logFile);
        FileUtil.appendUtf8Lines(Collections.singletonList(DateUtil.format(new Date(), DateConstants.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS) + "  error: " + content), logFile);
        log.error(content);
    }
}
